package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.AuthRoleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdAuthRoleService;
import com.onluy.sysman.entity.sysmgr.TdAuthRole;

@Validated
@RestController
@RequestMapping("/authrole")
@Api(value="authrole", description="/角色管理接口", position=1 )
public class TdAuthRoleController extends AppWalkController {


	//调用服务
	@Autowired
	private TdAuthRoleService tdAuthRoleService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据 areaCode,sysCode,roleCode  获取信息")
	public RspInfo<TdAuthRole> dotarget(@Valid @RequestBody ReqInfo<ReqHead, AuthRoleParam> reqInfo   ) throws Exception {
		RspInfo<TdAuthRole> jsonResult = new RspInfo<TdAuthRole>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdAuthRole tdAuthRole = new TdAuthRole();


		tdAuthRole.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdAuthRole.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdAuthRole.setRoleCode(reqInfo.getReqBody().getRoleCode()).asCondition();


		tdAuthRole = tdAuthRoleService.model(tdAuthRole);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdAuthRole);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdAuthRole> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdAuthRole tdAuthRole = reqInfo.getReqBody();
		num = tdAuthRoleService.add(tdAuthRole);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据 areaCode,sysCode,roleCode 更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdAuthRole> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;

		TdAuthRole tdAuthRole = reqInfo.getReqBody();

		tdAuthRole.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdAuthRole.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdAuthRole.setRoleCode(reqInfo.getReqBody().getRoleCode()).asCondition();

		num=tdAuthRoleService.update( tdAuthRole);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据  areaCode,sysCode,roleCode  删除")
	public RspInfo<InParam> doDel( @Valid @RequestBody    ReqInfo<ReqHead,AuthRoleParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		TdAuthRole tdAuthRole = new TdAuthRole();
		int num = 1;


		tdAuthRole.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdAuthRole.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdAuthRole.setRoleCode(reqInfo.getReqBody().getRoleCode()).asCondition();

		num = tdAuthRoleService.del(tdAuthRole);

		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		} else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="列表明细")
	public RspInfo<PageData<TdAuthRole>>  doList(@RequestBody    ReqInfo<ParamPagination, TdAuthRole> reqInfo )throws Exception {
		RspInfo<PageData<TdAuthRole>> jsonResult=new RspInfo<PageData<TdAuthRole>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件


		PageData<TdAuthRole> list =tdAuthRoleService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdAuthRole tdAuthRole){

		if(tdAuthRole == null )return;

		if(null!=tdAuthRole.getAreaCode()){
			tdAuthRole.setAreaCode(tdAuthRole.getAreaCode()).asCondition();
		}if(null!=tdAuthRole.getSysCode()){
			tdAuthRole.setSysCode(tdAuthRole.getSysCode()).asCondition();
		}if(null!=tdAuthRole.getRoleCode()){
			tdAuthRole.setRoleCode(tdAuthRole.getRoleCode()).asCondition();
		}
		if(null!=tdAuthRole.getRoleState()){
			tdAuthRole.setRoleState(tdAuthRole.getRoleState()).asCondition();
		}
		if(null!=tdAuthRole.getRoleType()){
			tdAuthRole.setRoleType(tdAuthRole.getRoleType()).asCondition();
		}

		if(null!=tdAuthRole.getRoleName()){
			tdAuthRole.setRoleName(null).getCondition().andLike( "%"+tdAuthRole.getRoleName()+"%" );
		}
		if(null!=tdAuthRole.getRoleMemo()){
			tdAuthRole.setRoleMemo(null).getCondition().andLike( "%"+tdAuthRole.getRoleMemo()+"%" );
		}

	}


}
